﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SLRT.business.api;
using System.Drawing;
using System.Runtime.InteropServices;
using Touchless.Vision.Camera;
using System.Windows;

namespace SLRT.business.src
{
    class EyeWebcam : IEyeWebcam
    {

        private CaptureModel _captureModel;
        private IBrain _brain;
        private CameraFrameSource _frameSource;
        private Bitmap _image;
        private int _webcamIndex;
        private int _width;
        private int _height;

        #region Properties

        public IBrain Brain
        {
            get { return _brain; }
            set { _brain = value; }
        }

        public int WebcamIndex
        {
            get { return _webcamIndex; }
            set { _webcamIndex = value; }
        }

        public int Width
        {
            get { return _width; }
            set { _width = value; }
        }

        public int Height
        {
            get { return _height; }
            set { _height = value; }
        }
        

        public Bitmap Image
        {
            get { return _image; }
            set { _image = value; }
        }

        #endregion

        /// <summary>
        /// Default Constructor with width = 320 and height = 240
        /// </summary>
        /// <param name="selectedCameraIndex">Set the selected camera index</param>
        public EyeWebcam(int selectedCameraIndex)
        {
            this._webcamIndex = selectedCameraIndex;
            this._captureModel = CaptureModel.Instance;
            this._width = 320;
            this._height = 240;
            this._image = new Bitmap(320,240);
        }

        /// <summary>
        /// Constructor with width and height
        /// </summary>
        /// <param name="width">Set the width for capturing</param>
        /// <param name="height">Set the height for capturing</param>
        /// <param name="selectedCameraIndex">Set the selected camera index</param>
        public EyeWebcam(int width, int height, int selectedCameraIndex)
        {
            this._webcamIndex = selectedCameraIndex;
            this._captureModel = CaptureModel.Instance;
            this._width = width;
            this._height = height;
            _image = new Bitmap(width, height);
        }

        public void OnImageCaptured(Touchless.Vision.Contracts.IFrameSource frameSource, Touchless.Vision.Contracts.Frame frame, double fps)
        {
            _image = frame.Image;
            this.NotifyWaitingComponents();
        }

        private void setFrameSource(CameraFrameSource cameraFrameSource)
        {
            if (_frameSource == cameraFrameSource)
                return;
            _frameSource = cameraFrameSource;
        }

        public void StartEyeWebcam() 
        {
            try
            {
                Camera c = CameraService.AvailableCameras.ElementAt(this._webcamIndex);
                setFrameSource(new CameraFrameSource(c));
                _frameSource.Camera.CaptureWidth = this._width;
                _frameSource.Camera.CaptureHeight = this._height;
                _frameSource.Camera.Fps = 20;
                Graphics g = Graphics.FromImage(_image);
                g.FillRectangle(Brushes.White, 0, 0, this._width, this._height);
                _frameSource.NewFrame += OnImageCaptured;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            if (!_frameSource.IsCapturing)
            {
                _frameSource.StartFrameCapture();
            }
        }

        public void StopEyeWebcam() 
        {
            if (_frameSource.IsCapturing)
            {
                _frameSource.StopFrameCapture();
            }
            Graphics g = Graphics.FromImage(_image);
            g.FillRectangle(Brushes.White, 0, 0, _image.Width, _image.Height);
            _frameSource.NewFrame += OnImageCaptured;
        }

        public void NotifyWaitingComponents()
        {
            //_brain.CapturedImageChanged = true;
            CaptureModel.EyeWebcamResultReceived = true;
        }
    }
}
